from pydantic import BaseModel, Field, EmailStr
from typing import Optional
from datetime import datetime
from src.utils.response import ResponseModel


class UserBase(BaseModel):
    """用户基础模型"""
    username: Optional[str] = Field(None, max_length=50, description="用户名")
    nick_name: Optional[str] = Field(None, max_length=100, description="昵称")
    phone: Optional[str] = Field(None, max_length=17, description="电话号码")
    email: Optional[EmailStr] = Field(None, description="邮箱")
    avatar: Optional[str] = Field(None, max_length=200, description="头像")
    sex: Optional[int] = Field(None, description="性别")
    remark: Optional[str] = Field(None, max_length=255, description="备注")


class UserCreate(UserBase):
    """创建用户请求模型"""
    username: str = Field(..., min_length=1, max_length=50, description="用户名")
    password: str = Field(..., min_length=6, max_length=100, description="密码")
    nick_name: Optional[str] = Field(None, max_length=100, description="昵称")


class UserUpdate(UserBase):
    """更新用户请求模型"""
    password: Optional[str] = Field(None, min_length=6, max_length=100, description="密码")


class UserLogin(BaseModel):
    """用户登录请求模型"""
    username: str = Field(..., description="用户名")
    password: str = Field(..., description="密码")


class UserChangePassword(BaseModel):
    """修改密码请求模型"""
    old_password: str = Field(..., description="旧密码")
    new_password: str = Field(..., min_length=6, max_length=100, description="新密码")


class UserInfo(UserBase):
    """用户信息响应模型"""
    id: int = Field(..., description="用户ID")
    open_id: Optional[str] = Field(None, description="微信open id")
    union_id: Optional[str] = Field(None, description="微信开放平台unionID")
    last_login_ip: Optional[str] = Field(None, description="最后一次登录IP")
    last_login_time: Optional[datetime] = Field(None, description="最后一次登录时间")
    status: Optional[int] = Field(1, description="状态：1为正常，0为禁止")
    create_time: datetime = Field(..., description="创建时间")
    create_by: Optional[int] = Field(None, description="创建者")
    update_time: datetime = Field(..., description="更新时间")
    update_by: Optional[int] = Field(None, description="更新者")
    del_flag: str = Field("0", description="删除标志（0代表存在 2代表删除）")

    model_config = {
        "from_attributes": True,
        "json_encoders": {
            datetime: lambda v: v.isoformat() if v else None
        }
    }


class UserInfoSimple(BaseModel):
    """用户简单信息模型"""
    id: int = Field(..., description="用户ID")
    username: Optional[str] = Field(None, description="用户名")
    nick_name: Optional[str] = Field(None, description="昵称")
    avatar: Optional[str] = Field(None, description="头像")
    status: Optional[int] = Field(1, description="状态")

    model_config = {"from_attributes": True}


class UserListQuery(BaseModel):
    """用户列表查询参数"""
    page: int = Field(1, ge=1, description="页码")
    page_size: int = Field(10, ge=1, le=100, description="每页数量")
    username: Optional[str] = Field(None, description="用户名筛选")
    nick_name: Optional[str] = Field(None, description="昵称筛选")
    email: Optional[str] = Field(None, description="邮箱筛选")
    phone: Optional[str] = Field(None, description="手机号筛选")
    status: Optional[int] = Field(None, description="状态筛选")


class UserListItem(BaseModel):
    """用户列表项模型"""
    id: int = Field(..., description="用户ID")
    username: Optional[str] = Field(None, description="用户名")
    nick_name: Optional[str] = Field(None, description="昵称")
    phone: Optional[str] = Field(None, description="电话号码")
    email: Optional[str] = Field(None, description="邮箱")
    avatar: Optional[str] = Field(None, description="头像")
    sex: Optional[int] = Field(None, description="性别")
    last_login_time: Optional[datetime] = Field(None, description="最后一次登录时间")
    status: Optional[int] = Field(1, description="状态")
    create_time: datetime = Field(..., description="创建时间")

    model_config = {
        "from_attributes": True,
        "json_encoders": {
            datetime: lambda v: v.isoformat() if v else None
        }
    }


class UserListResponse(BaseModel):
    """用户列表响应模型"""
    items: list[UserListItem] = Field(..., description="用户列表")
    total: int = Field(..., description="总数量")
    page: int = Field(..., description="当前页码")
    page_size: int = Field(..., description="每页数量")
    total_pages: int = Field(..., description="总页数")


class Token(BaseModel):
    """Token响应模型"""
    access_token: str = Field(..., description="访问令牌")
    token_type: str = Field("bearer", description="令牌类型")
    expires_in: int = Field(..., description="过期时间（秒）")


class TokenData(BaseModel):
    """Token数据模型"""
    user_id: Optional[int] = None
    username: Optional[str] = None


# 响应模型
class UserInfoResponse(ResponseModel[UserInfo]):
    """用户信息响应模型"""
    pass


class UserListResponseModel(ResponseModel[UserListResponse]):
    """用户列表响应模型"""
    pass


class TokenResponse(ResponseModel[Token]):
    """Token响应模型"""
    pass 